package soundChaser;

import programm.OwnFileChooser;
import FFT.FFTAnalyzer;

public class Testmain
{

	/**
	 * MAIN
	 * 
	 * @throws InterruptedException
	 */
	public static void main(String[] args) throws InterruptedException 
	{
		String mp3ort = (new OwnFileChooser(".mp3",
				"Bitte eine MP3 Datei waehlen!").getPath());
		WaveLesen wlObject = new WaveLesen(mp3ort);
		
		//get the temp folder
		String tmpDir = System.getProperty("java.io.tmpdir");
		tmpDir = tmpDir.replaceAll("\\\\", "/");
		//if there is no file seperator at the end set it
		if(tmpDir.lastIndexOf("/") != tmpDir.length() - 1)
			tmpDir += "/";
		
		//replace the .mp3 ending with .wav
		String filename = mp3ort.substring(mp3ort.lastIndexOf("/") + 1);
		filename = filename.substring(0, filename.lastIndexOf(".")) + ".wav";
		
		//save the wave file in the temp folder
		String waveort;
		waveort = wlObject.convert(tmpDir + filename);
		
	    /*String mp3ort = (new OwnFileChooser(".mp3",
				"Bitte eine MP3 Datei waehlen!").getPath());
		System.out.println(mp3ort);
		
		
		//autocorrelation lol = new autocorrelation();
		AudioPlayer player = new AudioPlayer(mp3ort);
		player.start(); // neuer Thread

		System.out.println("Position: " + player.getPosition());
		//TODO: lieder ueber 4:30 Minuten
		WaveLesen aa = new WaveLesen(mp3ort);
		
		String filename = mp3ort.substring(mp3ort.lastIndexOf("/") + 1);
		filename = filename.substring(0, filename.lastIndexOf(".")) + ".wav";
		
		System.out.println(filename);
		
		String waveort;
		waveort = aa.convert("/tmp/" + filename);
		//waveort = aa.convert();
		
		
		double[][] wavearray = aa.wave2array(waveort);
		
		System.out.println(wavearray[0][100]);

		AudioTime timegetter = new AudioTime(player);
		timegetter.start();

		
		JOCHEN
		//Thread fftAnalyzer
		FFTAnalyzer fft = new FFTAnalyzer(wavearray[0].length, aa.getSampleRate());
		AutoCorrelation ac = new AutoCorrelation(wavearray[0].length, aa.getSampleRate());
		ac.run(wavearray, aa.getSampleRate());
		fft.run(wavearray, aa.getSampleRate());
		ac.start();
		fft.start();
		ac.join();
		fft.join();
		
		double[][] mutter = ac.ergebnisArray;
		double[][][] junge = fft.ergebnisArray;
		
		double max = 0;
		int intesitaet = 0;
		int rot = 0;
		int gruen = 0;
		int blau = 0;		
		
		//find the max and the min of autocorrelation
		
		for(int i = 0; i<mutter[0].length; i++)
		{
			if(max<mutter[0][i])
			{
				max = mutter[0][i];
			}
			if(max<mutter[1][i])
			{
				max = mutter[1][i];
			}
		}
		// schrittWeite is connected to max value 255
		for ( int i = 0; i< junge[0].length; i++)
		{
			intesitaet = (int) (255*mutter[0][i]/max);
			if (intesitaet<(int) (255*mutter[1][i]/max))
				intesitaet = (int) (255*mutter[1][i]/max);
			//if (junge)
		}
			

		System.out.println("länge array " + wavearray[0].length
				+ " Samplerate: " + aa.getSampleRate());
		for(int i = 0; i<junge[0][0].length; i++)
		{
			//System.out.println(junge[0][0][i] + " " + junge[1][0][i]);
			System.out.println(mutter[0][i] + " " + mutter[1][i]);
		}
		JOCHEN*/
		/*
		 * while (player.isAlive()) { System.out.println("Position in ms: " +
		 * player.getPosition()); }
		 */
	}
}